<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\doDeliver;
use App\Admin\Actions\QueryDeliver;
use App\Admin\Actions\UpdateAddress;
use App\Model\OrderGoods;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class OrderGoodsController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = 'OrderGoods';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {

        $orderStatus = [
            0=>'待付款',
            1=>'待发货',
            2=>'配送中',
            3=>'已完成'
        ];
        $grid = new Grid(new OrderGoods());
        $grid->model()->orderBy('order_id');
        $grid->column('user.id', __('用户id'));
        $grid->column('user.account', __('购买账号'));
        $grid->column('orderInfo.order_sn', __('订单编号'))->width(100);;
        $grid->column('orderInfo.order_status', __('订单状态'))->using($orderStatus);;
        $grid->column('goods_name', __('购买商品'))->width(100);
        $grid->column('goods_image', __('商品图片'))->image('', 80, 80);
        $grid->column('price', __('商品价格'));
        $grid->column('buy_num', __('购买数量'));
//        $grid->column('order_money', __('应付金额'));
        $grid->column('pay_money', __('实付金额'));
        $grid->column('created_at', __('下单时间'));
        $grid->column('orderInfo.address_info', __('收货地址'))->width(230);
        $grid->column('orderInfo.delivery_user', __('收货人'));
        $grid->column('orderInfo.phone', __('收货电话'));
        $grid->column('orderInfo.deliver_sn', __('物流详情'))->modal('物流详情', function ($model) {

            if($model['orderInfo']['deliver_info']){
                $deliverInfo = json_decode($model['orderInfo']['deliver_info'],true);
            }else{
                $deliverInfo = [];
            }
            if(!$deliverInfo){
                $deliverInfo = [];
            }
            $showHtml = '<p>当前状态：<span style="color: #0d6aad">'.($deliverInfo['theLastMessage']??'').'</span></p>';
            $showHtml.= '<hr/><p>物流动态：</p>';
            if(isset($deliverInfo['logisticsTraceDetailList'])){
                $changeList = $deliverInfo['logisticsTraceDetailList'];
                foreach ($changeList as $key=>$value){
                    $time = date('Y-m-d H:i:s',($value['time']/1000));
                    $showHtml.= "<p><span>{$time}</span></p><p>{$value['desc']}</p><p style='margin-top: 10px'></p>";
                }
            }
            return $showHtml;
//            return new Table(['时间', '物流变更'], $deliverInfo);
        });
        $grid->filter(function($filter){
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            // 在这里添加字段过滤器
            $orderStatus = [
                0=>'待付款',
                1=>'待发货',
                2=>'配送中',
                3=>'已完成'
            ];
            $filter->equal('orderInfo.order_status','订单状态')->select($orderStatus);
            $filter->equal('user.account','购买账号');

        });
        $grid->disableColumnSelector();
        $grid->disableRowSelector();
        $grid->disableCreateButton();
//        $grid->disableActions();
        $grid->actions(function ($actions) {

            // 去掉删除
            $actions->disableDelete();

            // 去掉编辑
            $actions->disableEdit();

//            // 去掉查看
            $actions->disableView();

            $actions->add(new doDeliver());
            $actions->add(new UpdateAddress());
            $actions->add(new QueryDeliver());
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(OrderGoods::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('order_id', __('Order id'));
        $show->field('sku_id', __('Sku id'));
        $show->field('goods_name', __('Goods name'));
        $show->field('goods_image', __('Goods image'));
        $show->field('price', __('Price'));
        $show->field('buy_num', __('Buy num'));
        $show->field('order_money', __('Order money'));
        $show->field('pay_money', __('Pay money'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new OrderGoods());

        $form->number('order_id', __('Order id'));
        $form->number('sku_id', __('Sku id'));
        $form->text('goods_name', __('Goods name'));
        $form->text('goods_image', __('Goods image'));
        $form->text('price', __('Price'));
        $form->number('buy_num', __('Buy num'));
        $form->decimal('order_money', __('Order money'));
        $form->decimal('pay_money', __('Pay money'));

        return $form;
    }

}
